Project

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(p8105.datasets)

1
## [1] 1
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout

Focus on NYC Airbnb data

library(p8105.datasets)

data("instacart")

instacart = instacart |> 
  janitor::clean_names() |> 
  mutate(
    order_dow = 
      case_match(
        order_dow, 
        1 ~ "Mon", 
        2 ~ "Tues",
        3 ~ "Wed",
        4 ~ "Thurs",
        5 ~ "Fri",
        6 ~ "Sat",
        0 ~ "Sun"),
    order_dow = as.factor(order_dow)) |> 
  mutate(day_week = forcats::fct_relevel(order_dow, c("Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun")))

A line plot showing the counts of items in each aisle

instacart |> 
  count(aisle) |> 
  filter(n > 10000) |> 
  mutate(aisle = fct_reorder(aisle, n)) |> 
  mutate(rank = min_rank(desc(n))) |> 
  arrange(desc(n)) |>
  plot_ly(
    x = ~aisle, y = ~n, type = "scatter", mode = "markers",
    alpha = 0.5)

A bar plot show what aisle had the most sale during a week.

try_df =
  instacart |> 
  count(aisle) |> 
  filter(n > 10000) 
  
bar_plot =  
  inner_join(try_df, instacart, by = "aisle") |> 
  mutate(order_dow = fct_reorder(order_dow, n)) |> 
  plot_ly(x = ~order_dow, y = ~n, color = ~order_dow, type = "bar", colors = "viridis")  

bar_plot

A scatter plot!

instacart |> 
  group_by(order_dow, order_hour_of_day) |> 
  summarise(avg_orders = n()) |> 
  arrange(order_dow,
  order_hour_of_day) |> 
  plot_ly(x= ~order_hour_of_day,
          y= ~avg_orders,
          color = ~order_dow,
          type = "scatter", mode = "markers",
     text = ~order_hour_of_day, alpha = 0.5)
## `summarise()` has grouped output by 'order_dow'. You can override using the
## `.groups` argument.